﻿using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Data;

namespace HelloWorldBll.Helper
{
    /// <summary>
    /// 操作正则表达式的公共类
    /// </summary>    
    public class RegexHelper
    {
        #region 验证输入字符串是否与模式字符串匹配
        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式字符串</param>        
        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="pattern">模式字符串</param>
        /// <param name="options">筛选条件</param>
        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(input, pattern, options);
        }
        #endregion
        public static  string FilterString(string inputstring)
        {
            var pat = @"on\w+=\s*(['""\s]?)([/s/S]*[^\1]*?)\1[\s]*";
            inputstring = Regex.Replace(inputstring, pat, string.Empty, RegexOptions.Compiled);
            pat = @"<[^>]*script[^>]*>((\n|.)*)<\/[^>]*script[^>]*>";
            inputstring = Regex.Replace(inputstring, pat, string.Empty, RegexOptions.Compiled);
            pat = "<(/){0,1}span[^<>]*>";
            inputstring = Regex.Replace(inputstring, pat, string.Empty, RegexOptions.Compiled)
            return inputstring;
        }
        public static List<string> GetUrl(string htmlstr)
        {
            List<string> list = new List<string>();
            var zz = new Regex("\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))");
            var mc = zz.Matches(htmlstr);//满足pattern的匹配集合        
            foreach (Match match in mc)
            {
                list.Add(match.Value.Replace(" href=\"", "").Replace("\"", "").Trim());                   
            }
            return list;
        }
    }
}
